﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.DAL.DBView
{
    public class SqlBulkHelper
    {
        public static bool Save(DataTable dt,string tablename)
        {
            string strConn = Config.GetConnStringByName("MTQFramework");
           
            //插入操作
            using (SqlConnection destinationConnection = new SqlConnection(strConn))
            {
                destinationConnection.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
                {
                    try
                    {
                        bulkCopy.DestinationTableName = tablename;//要插入的表的表名  

                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            string item = dt.Columns[i].ColumnName;
                            bulkCopy.ColumnMappings.Add(item, item);
                        }
                        bulkCopy.WriteToServer(dt);
                    }
                    catch (Exception ex)
                    {
                        

                        throw ex;
                    }
                }
            }
            return true;

        }
    }
}
